package jvlite.aop;

import java.sql.Connection;
import java.sql.SQLException;

import jvlite.dao.dataSource.ConnectionUtil;
import jvlite.mvc.annotaion.Part;

@Part
public class TransactionAdvice implements IAdvice {
	
	private static Connection conn;
//	 static int TRANSACTION_NONE = 0;
//	 static int TRANSACTION_READ_UNCOMMITTED = 1;
//	 static int TRANSACTION_READ_COMMITTED = 2;
//	 static int TRANSACTION_REPEATABLE_READ = 4;
//	 static int TRANSACTION_SERIALIZABLE = 8;	
	@Override
	public void beforeMethod(Object method) {
		try {
			conn = ConnectionUtil.getConn();
			conn.setAutoCommit(false);
			conn.setTransactionIsolation(1);
//			conn.setReadOnly(true);
			conn.setSavepoint();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void afterMethod(Object method) {
		try {
			conn.commit();			
		} catch (Exception e) {
			e.printStackTrace();
			try {
				conn.rollback();
				conn.commit();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void whenException(Exception e) {
		e.printStackTrace();
		try {
			conn.rollback();
			conn.commit();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
	}

}